﻿module Sustainalytics.EsgRatings {

    import LRTM = Sustainalytics.LRTM;

    export class WeightMatricesController {
        private weightMatricesService: any;
        private longRunningTaskManagerService: LRTM.LongRunningTaskManagerService;
        public selectedFile: any;
        public loading: boolean;
        public $scope: ng.IScope;
        public $rootScope: any;
        private $q: ng.IQService;
        public defaultWeightMatrix: WeightMatrix;
        public notificationFactory: INotificationFactory;
        public uploadDetails: string;

        constructor($scope: ng.IScope, $rootScope, $q: ng.IQService, $location: ng.ILocationService, $http: ng.IHttpService, WeightMatricesService, LongRunningTaskManagerService, notificationFactory: INotificationFactory, staticObjects) {
            $scope['weightMatrices'] = this;

            this.$q = $q;
            this.$scope = $scope;
            this.$rootScope = $rootScope;
            this.weightMatricesService = WeightMatricesService;
            this.longRunningTaskManagerService = LongRunningTaskManagerService;
            this.notificationFactory = notificationFactory;

            this.initialize();
        }

        initialize(): ng.IPromise<any> {
            this.weightMatricesService
                .listWeightMatricesByAccountId("00000000-0000-0000-0000-000000000000")
                .then(response => {
                    if (response.data.length == 1) this.defaultWeightMatrix = response.data[0];
                });

            return this.$q.defer().promise;
        }

        public selectFile($files) {
            this.selectedFile = $files[0];

            if (!this.selectedFile) {
                this.uploadDetails = null;
            }
        }

        public uploadWeightMatrix() {
            this.uploadDetails = null;

            if (this.selectedFile) {
                if (this.selectedFile.type != "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" &&
                    this.selectedFile.type != "application/vnd.ms-excel") {
                    alert("File format not supported!");
                    this.selectedFile = null;                    
                }
                else {
                    this.$rootScope.loading = true;

                    var fileReader = new FileReader();
                    fileReader.onload = (rootScope => {
                        return e => this.weightMatricesService.uploadWeightMatrix(e.target.result, this.selectedFile.name, this.selectedFile.type)
                                        .then(() => alert("The weight matrix was successfully uploaded for processing.")/* succes handler */
                            , response => response.data.fileUploadLogs != null ? this.uploadDetails = response.data.fileUploadLogs.errors/* error handler */
                                                                                : alert("Unexpected error encountered on the server while trying to process the uploaded file!"))
                                        .finally(() => rootScope.loading = false);
                    })(this.$rootScope);

                    fileReader.readAsArrayBuffer(this.selectedFile);
                }
            }
        }

        public downloadDefaultWeightMatrix() {
            this.addDownloadTask({ weightMatrixId: this.defaultWeightMatrix.id }, this.defaultWeightMatrix.name);
        }

        private addDownloadTask(downloadParams: WeightMatrixDownloadTaskParams, weightMatrixName: string) {
            this.longRunningTaskManagerService.runTask(weightMatrixName + " " + moment().format("YYYY-MM-DD_HH-mm-ss-SSS"),
                JSON.stringify(downloadParams),
                LRTM.LongRunningTaskTypeEnum.WeightMatrixDownload, true, null);
        }
    }

    // register controller with EsgRatings Angular module
    angular.module('Sustainalytics.EsgRatings').controller('WeightMatricesController', ($scope: ng.IScope, $rootScope, $q: ng.IQService, $location: ng.ILocationService, $http: ng.IHttpService, WeightMatricesService, LongRunningTaskManagerService: LRTM.LongRunningTaskManagerService, notificationFactory: INotificationFactory, staticObjects: IStaticObjects) => new WeightMatricesController($scope, $rootScope, $q, $location, $http, WeightMatricesService, LongRunningTaskManagerService, notificationFactory, staticObjects));
} 